package com.qianchang.microfinance.view.credit.data;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.BitmapUtils;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.FindUserBasicInfoBean;
import com.qianchang.microfinance.bean.LoanType;
import com.qianchang.microfinance.bean.UploadFileBean;
import com.qianchang.microfinance.bean.UserLoginInfo;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.ImageChoseActivity;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by SSC on 2017/12/27.
 *
 *  工作证明页面
 *
 */

public class WorkProofActivity extends ImageChoseActivity implements ImageChoseActivity.UpLoadFileCallback{

    @BindView(R.id.iv_positive_img)
    ImageView workImg;
    @BindView(R.id.iv_positive_add)
    ImageView posImg;

    private String file;


    private FindUserBasicInfoBean basicInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_workproof_identity);
        ButterKnife.bind(this);
        initView();
        initData();

    }

    @Override
    protected void initView() {
        super.initView();
        String title = getString(R.string.work_proof);
        setTitle(title);
        setMaxImageCount(1);
        setUpLoadCallBackListener(this);
        Bundle bundle = getBundle();
        if (null != bundle) {
            basicInfoBean = (FindUserBasicInfoBean) bundle.getSerializable(ConstantAppInfo.BASIC_INFO_BEAN);
            if (null != basicInfoBean) {
                refreshView();
            }
        }
        if (basicInfoBean == null) {
            basicInfoBean = new FindUserBasicInfoBean();
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    private void refreshView() {
        switch (basicInfoBean.getIsIdCardCheckThrough()) {//实名认证是否通过，0：是、1：否
            case 0:   //已经通过 只能预览

                break;
            case 1:

                break;
            default:
                break;
        }
    }


    @OnClick({R.id.iv_positive_add,R.id.btn_workproof_photo_commit,R.id.iv_positive_img})
    public void onViewClicked(View view){
        switch (view.getId()) {
            case R.id.iv_positive_add:
                openCamora();
                break;
            case R.id.iv_positive_img:
                openCamora();
                break;
            case R.id.btn_workproof_photo_commit:

                uploadFileWorkProof(file);

                break;
            default:
                break;
        }
    }

    private void openCamora() {
        initImagePickerForTakePhoto();
        Intent intent = new Intent(this, ImageGridActivity.class);
        intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
        startActivityForResult(intent, REQUEST_CODE_SELECT);
    }



    @Override
    public void onPhotoResult(ArrayList<ImageItem> imageItems, int flag) {
        if (null != imageItems && imageItems.size() > 0) {
            ImageItem imageItem = imageItems.get(0);
            String filePath = imageItem.getPath();
            refreshView(filePath);
            Glide.with(WorkProofActivity.this).load(filePath).error(R.drawable.img_work_bg).into(workImg);
            file = BitmapUtils.createFile(this, filePath);
//            uploadFile(file);
            clearSelectImages();
        }
    }

    private void refreshView(String filePath){
        if(filePath != null && !(filePath.equals(""))){
            posImg.setVisibility(View.GONE);

        }else{
            posImg.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onUpLoadSuccess(UploadFileBean uploadFileBean, String imagePath, int flag) {

    }



    private void uploadFileWorkProof(final String imagePath){
        if (null == imagePath) {
            return;
        }

        UserLoginInfo userLoginInfo = DMApplication.getInstance().getmUserLoginInfo();
        String usid = userLoginInfo.getUserId();
        File file = new File(imagePath);
        Map<String, File> files = new HashMap<>();
        files.put(file.getName(), file);
        Map<String, String> fileParamsMap = new HashMap<>();
        fileParamsMap.put("allowFile", "jpg,png");
        fileParamsMap.put("fileType", "0");
        fileParamsMap.put("isTransform", "false");
        fileParamsMap.put("userId",usid);
        show();

        DMOKHttpUtils.getInstance().uploadFile(this, DMConstant.ApiURL.EXECUTE_SENDVERIFYPROOF, fileParamsMap, "uploadFile", files, new RequestCallBack<Object>() {

            @Override
            public void onSuccess(JsonResult<Object> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
//                    ToastUtil.getInstant().show(WorkProofActivity.this, getString(R.string.commit_success));
//                    WorkProofActivity.this.finish();


                    Bundle bundle = new Bundle();
                    bundle.putString(ConstantAppInfo.PROJECT_TYPE, LoanType.JBD);
                    startActivity(ConfirmInfoJinbaoActivity.class, bundle,true);

                } else {
                    ToastUtil.getInstant().show(WorkProofActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(WorkProofActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(WorkProofActivity.this, msg);
            }
        });


    }



}
